package com.genghis.ptas.friend;

import com.genghis.ptas.friend.entity.Friend;
import com.genghis.ptas.friend.entity.FriendChatRecord;
import com.genghis.ptas.friend.service.FriendService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("/friend/")
public class FriendController {
    @Autowired
    private FriendService friendService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "listAllFriendDo", method = {RequestMethod.GET})
    public String listAllFriendDo(HttpServletRequest request, Friend friend) {
        List<User> friends = friendService.findFriends(friend);
        request.setAttribute("friendName", friend.getFriendName());
        request.setAttribute("friends", friends);
        return "frontstage/friend/listAllFriendDo";
    }

    @RequestMapping(value = "listAllFriend", method = {RequestMethod.POST})
    @ResponseBody
    public List<Friend> listAllFriend(HttpServletRequest request, Friend friend) {
        return friendService.listAllFriend(request.getSession(), friend);
    }

    @RequestMapping(value = "addFriendDo", method = {RequestMethod.GET})
    public String addFriendDo(HttpServletRequest request, @RequestParam("id") int typeId) {
        return "frontstage/friend/addFriendDo";
    }

    @RequestMapping(value = "addFriend", method = {RequestMethod.POST})
    @ResponseBody
    public boolean addFriend(HttpSession session, Friend friend) {
        try {
            User user = (User) session.getAttribute("user");
            friend.setUser(user);
            friendService.addFriend(friend);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @RequestMapping(value = "delFriend", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delFriend(HttpServletRequest request, Friend friend) {
    }

    @RequestMapping(value = "dealFriendApply", method = {RequestMethod.POST})
    @ResponseBody
    public boolean dealFriendApply(HttpSession session, Friend friend) {
        try {
            User user = (User) session.getAttribute("user");
            friend.setUser(user);
            friendService.changeApplyState(friend);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @RequestMapping(value = "chatWithFriendDo", method = {RequestMethod.GET})
    public String chatWithFriend(HttpServletRequest request, Friend friend) {
        User friendUser = userService.findUserAllInfoById(friend.getFriendId());
        User user = userService.findFullUserByUserLoginName(LoginUserUtil.findLoginUserName());
        friend.setUserId(user.getId());
        int friendGroupId = friendService.findFriendGroupId(friend);
        List<FriendChatRecord> friendChatRecords = friendService.findChatRecord(new FriendChatRecord(DateUtil.getAgoDate(Calendar.HOUR, -6), friendGroupId));
        String lastTime = friendChatRecords.size() == 0 ? "" : friendChatRecords.get(friendChatRecords.size() - 1).getSendDate();
        request.setAttribute("friendUser", friendUser);
        request.setAttribute("user", user);
        request.setAttribute("friend", friend);
        request.setAttribute("friendGroupId", friendGroupId);
        request.setAttribute("friendChatRecords", friendChatRecords);
        request.setAttribute("lastTime", lastTime);
        return "frontstage/friend/chatWithFriendDo";
    }

    @RequestMapping(value = "addChatRecord", method = {RequestMethod.POST})
    @ResponseBody
    public void addChatRecord(FriendChatRecord friendChatRecord) {
        friendService.addChatRecord(friendChatRecord);
    }

    @RequestMapping(value = "getNewRecord", method = {RequestMethod.POST})
    @ResponseBody
    public List<FriendChatRecord> getNewRecord(FriendChatRecord friendChatRecord) {
        return friendService.findChatRecord(friendChatRecord);
    }

    @RequestMapping(value = "myFriendDo", method = {RequestMethod.GET})
    public String myFriendDo(HttpServletRequest request, Friend friend) {
        HttpSession session = request.getSession();
        List<Friend> friendList = friendService.listAllFriend(session, friend);
        request.setAttribute("friendList", friendList);
        return "frontstage/person/myFriendDo";
    }
}
